package com.qyer.lxp.common.framework.convert;

import com.qyer.lxp.common.framework.httpclient.HttpClientUtil;
import com.qyer.lxp.model.availability.normal.Available;
import com.qyer.lxp.model.availability.normal.UnitPrice;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import java.io.StringReader;
import java.io.StringWriter;
import java.io.Writer;
import java.util.ArrayList;
import java.util.List;

/**
 * XML的帮助类
 * Created by jiangping on 16/5/9.
 */
public class XmlHelper {
    /**
     * 将自定义数据对象转化为XML字符串
     *
     * @param clazz 自定义数据类型
     * @param object 自定义数据对象
     * @return XML字符串
     * @throws JAXBException 异常
     */
    public static String objectToXML(Class<?> clazz, Object object)
            throws JAXBException
    {
        String xml = null;
        JAXBContext context = JAXBCache.instance().getJAXBContext(clazz);
        Marshaller m = context.createMarshaller();
        m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
        Writer w = new StringWriter();
        m.marshal(object, w);
        xml = w.toString();
        return xml;
    }

    /**
     * 将XML字符串转化为自定义数据对象
     *
     * @param clazz 自定义数据类型
     * @param xml XML字符串
     * @return 自定义数据对象
     * @throws JAXBException 异常
     */
    public static Object xmlToObject(Class<?> clazz, String xml)
            throws JAXBException
    {
        JAXBContext context = JAXBCache.instance().getJAXBContext(clazz);
        Unmarshaller um = context.createUnmarshaller();
        return um.unmarshal(new StringReader(xml));
    }

    public static void main(String[] args) {
        Available available=new Available();
        UnitPrice unitPrice = null;
        List<UnitPrice> unitPriceList=new ArrayList<UnitPrice>();
        available.setAvailable(false);
        available.setRemark("jfafasdfjkdjfi");
        available.setTotalNetPrice(400);
        unitPrice = new UnitPrice();
        unitPrice.setCode("123879174917293871");
        unitPrice.setPrice(200);
        unitPriceList.add(unitPrice);
        unitPrice = new UnitPrice();
        unitPrice.setCode("12387917492342893871");
        unitPrice.setPrice(200);
        unitPriceList.add(unitPrice);
        available.setUnitPrice(unitPriceList);

        try {
            String xml = XmlHelper.objectToXML(Available.class,available);
            System.out.println(xml);
        } catch (JAXBException e) {
            e.printStackTrace();
        }

        String xml1="<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n" +
                "<response>\n" +
                "  <status-code>200</status-code>\n" +
                "  <status-message>OK</status-message>\n" +
                "  <payload>\n" +
                "<available>\n" +
                "    <available>false</available>\n" +
                "    <remark>jfafasdfjkdjfi</remark>\n" +
                "    <total-net-price>400</total-net-price>\n" +
                "    <unit-price>\n" +
                "        <code>123879174917293871</code>\n" +
                "        <price>200</price>\n" +
                "    </unit-price>\n" +
                "    <unit-price>\n" +
                "        <code>12387917492342893871</code>\n" +
                "        <price>200</price>\n" +
                "    </unit-price>\n" +
                "</available>\n" +
                "</payload>\n" +
                "</response>";

        String xml2 = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n" +
                "<response>\n" +
                "  <status-code>200</status-code>\n" +
                "  <status-message>OK</status-message>\n" +
                "  <payload>\n" +
                "<available>\n" +
                "    <available>false</available>\n" +
                "    <remark>jfafasdfjkdjfi</remark>\n" +
                "    <total-net-price>400</total-net-price>\n" +
                "    <prices>\n" +
                "        <unit-price>\n" +
                "            <code>123879174917293871</code>\n" +
                "            <price>200</price>\n" +
                "        </unit-price>\n" +
                "        <unit-price>\n" +
                "            <code>12387917492342893871</code>\n" +
                "            <price>200</price>\n" +
                "        </unit-price>\n" +
                "    </prices>\n" +
                "</available>\n" +
                "</payload>\n" +
                "</response>";
        try {
            /*Available available1 = (Available) XmlHelper.xmlToObject(Available.class,xml1);
            System.out.println("**************************"+available1.getRemark());
            System.out.println("**************************"+available1.getTotalNetPrice());*/
            /*Response<Available> response = new Response<Available>();
            Available available = new Available();
            UnitPrice unitPrice = null;
            List<UnitPrice> unitPriceList=new ArrayList<UnitPrice>();
            response.setStatusCode(200);
            response.setStatusMessage("success");
            available.setAvailable(true);
            available.setTotalNetPrice(400);
            available.setRemark("jfafasdfjkdjfi");
            unitPrice = new UnitPrice();
            unitPrice.setCode("123879174917293871");
            unitPrice.setPrice(200);
            unitPriceList.add(unitPrice);
            unitPrice = new UnitPrice();
            unitPrice.setCode("12387917492342893871");
            unitPrice.setPrice(200);
            unitPriceList.add(unitPrice);
            available.setUnitPrice(unitPriceList);
            response.setPayload(available);
            JAXBContext context = JAXBContext.newInstance(Response.class,Available.class,UnitPrice.class);
            Marshaller m = context.createMarshaller();
            m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
            Writer w = new StringWriter();
            m.marshal(response, w);
            String xml = w.toString();
            System.out.println(xml);

            Unmarshaller um = context.createUnmarshaller();
            Response<Available> response1 = (Response<Available>) um.unmarshal(new StringReader(xml));
            System.out.println("***********************************");
            System.out.println(HttpClientUtil.removeHeader(xml1));*/
            xml2=HttpClientUtil.removeHeader(xml2);
            Available available1 =(Available) XmlHelper.xmlToObject(Available.class,xml2);
            System.out.println(available1);
            System.out.println(Available.class.getSimpleName());
        } catch (JAXBException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
